Skip to content

handle http error before closing the body#32

Merged
Rucknar merged 1 commit intogithubexporter:masterfrom
msiuts:fix_defer_body_close
Jan 31, 2019
Merged

handle http error before closing the body#32
Rucknar merged 1 commit intogithubexporter:masterfrom
msiuts:fix_defer_body_close

Conversation

@msiuts
Copy link
Copy Markdown
Contributor

@msiuts msiuts commented Jan 2, 2019

This should help with this:
panic: runtime error: invalid memory address or nil pointer dereference

See example in the http docs:
https://golang.org/pkg/net/http/

The client must close the response body when finished with it:

resp, err := http.Get("http://example.com/")
if err != nil {
	// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// ...

This should help with this:
panic: runtime error: invalid memory address or nil pointer dereference

See example in the http docs:
https://golang.org/pkg/net/http/
Copy link
Copy Markdown

@jlegrone jlegrone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If anyone else is wondering why not just defer immediately after declaring resp, this is a helpful quote from the docs:

On error, any Response can be ignored. A non-nil Response with a non-nil error only occurs when CheckRedirect fails, and even then the returned Response.Body is already closed.

A response is typically nil on error, so calling Body.Close() panics.

@msiuts
Copy link
Copy Markdown
Contributor Author

msiuts commented Jan 26, 2019

Could you please merge it and provide a fixed Docker image?

@Rucknar Rucknar merged commit 5111b3b into githubexporter:master Jan 31, 2019
@Rucknar
Copy link
Copy Markdown
Contributor

Rucknar commented Jan 31, 2019

Thanks all, i’ll Sort the docker image out today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants